
import { _decorator, Component, Node, Prefab, instantiate, Label, Button } from 'cc';
import { birdControl } from './birdControl';
const { ccclass, property } = _decorator;
 

enum GameStatus{
    pause = 1,
    start,
    end
}
@ccclass('birdManager')
export class birdManager extends Component {
    status: number = GameStatus.pause;
    // 小鸟预设体
    @property({type: Prefab})
    birdPre: Prefab;
    // 生成小鸟时间 秒
    @property
    time: number = 1;
    
    @property({type: Button})
    lblStart: Button;
    @property({type: Label})
    lblScore: Label;
    @property({type: Label})
    lblEnd: Label;
    // 分数
    score: number = 0;

    count: number = 0;

    startGame () {    
        if(this.status == GameStatus.pause){
            console.log("开始游戏！");
            // 开始游戏
            this.status = GameStatus.start;
            this.lblStart.node.active = false;
        }
    }

    update (deltaTime: number) {
        if(this.status != GameStatus.start) return;
        this.count ++;
        if(this.count == 30){
            this.count = 0;
            // 实例化小鸟
            let bird = instantiate(this.birdPre);
            // 设置父级
            bird.setParent(this.node);
            // 设置位置
            bird.setPosition(Math.random() * 200 - 10, this.node.position.y, 0);
            
            let birdCon = bird.getComponent(birdControl);
            // 加分回调
            birdCon.addScoreCallBack = () => {
                this.score += 100;
                this.lblScore.string = this.score + "";
                console.log("分数"+this.score);
            };
            // 游戏结束
            birdCon.dieCallBack = () => {
                this.node.destroyAllChildren();
                this.status = GameStatus.end;
                console.log("游戏结束！");
                this.lblEnd.node.active = true;
            };
        }
    }
}
